#pragma once

namespace Base { namespace ConstStr {

    template<class T>
    class ConstStrImpl
    {
    public:
        ConstStrImpl();
        ~ConstStrImpl();

        const T* string(const T&);

    public:
        typedef std::map<T, T*> Strings;

        Strings     m_strings;
    };

    template<class T>
    ConstStrImpl<T>::ConstStrImpl()
    {
    }

    template<class T>
    ConstStrImpl<T>::~ConstStrImpl()
    {
        for (Strings::iterator it = m_strings.begin();
             it != m_strings.end(); ++it)
        {
            delete it->second;
            it->second = 0;
        }

        m_strings.clear();
    }

    template<class T>
    const T*
    ConstStrImpl<T>::string(const T& str)
    {
        T* ptr = 0;

        Strings::const_iterator it = m_strings.find(str);
        if (it == m_strings.end())
        {
            ptr = new T(str);
            m_strings[str] = ptr;
        }
        else
        {
            ptr = it->second;
        }

        return ptr;
    }
}}